package com.gerfmarquez.creadormusica.sound;

import com.gerfmarquez.creadormusica.utilities.Constants;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.SystemClock;
import android.util.Log;

public class TempoManager {
    AudioTrack firstNote;
    AudioTrack secondNote;
    AudioTrack fourthNote;
    AudioTrack eighthNote;
    
    private static final String TAG = "TempoManager";
    private static TempoManager tmpM = null;
    
    private int bpm;
    private long bpmMillisec;
    private int noteDuration;
    
    private TempoManager(int bpm) {
    	this.bpm = bpm;
    	bpmMillisec = bpm*1000;
    	
    	firstNote = new AudioTrack( AudioManager.STREAM_MUSIC,  44100,
    			AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT,
//    			Constants.ONE_SEC_WAV_BYTES, AudioTrack.MODE_STATIC);
    			Constants.MULTIPLE_FIRST*bpm, AudioTrack.MODE_STATIC);
    	secondNote = new AudioTrack( AudioManager.STREAM_MUSIC,  44100,
    			AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT,
//    			Constants.ONE_SEC_WAV_BYTES, AudioTrack.MODE_STATIC);
    			Constants.MULTIPLE_SECOND*bpm, AudioTrack.MODE_STATIC);
//    	fourthNote = new AudioTrack( AudioManager.STREAM_MUSIC,  44100,
//    			AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT,
////    			Constants.ONE_SEC_WAV_BYTES, AudioTrack.MODE_STATIC);
//    			Constants.MULTIPLE_FIRST*bpm, AudioTrack.MODE_STATIC);
//    	eighthNote = new AudioTrack( AudioManager.STREAM_MUSIC,  44100,
//    			AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT,
////    			Constants.ONE_SEC_WAV_BYTES, AudioTrack.MODE_STATIC);
//    			Constants.MULTIPLE_SECOND*bpm, AudioTrack.MODE_STATIC);
    }
    public static TempoManager getInstance(int bpm) {
          if(tmpM == null) {
        	  tmpM =  new TempoManager(bpm);
          }
          return tmpM;
    }
    public void playNote(byte []soundX, Constants.Note note) {
        if(soundX != null) {
//        	final int minSize = AudioTrack.getMinBufferSize( 44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT );

//        	track.play();
//        	track.setPlaybackRate(88200);
        	
//        	track.flush();
        	Log.d(TAG, "Play State: "+secondNote.getPlayState());
        	int result = secondNote.write(soundX, 0, soundX.length);
        	Log.d(TAG, "Result of writing audio: "+result);
        	long millisec = SystemClock.uptimeMillis();
        	long toMillisec = millisec+1000;
        	Log.e(TAG, "Up time millis : "+millisec + " +1 sec: " +toMillisec);
        	secondNote.play();
        	
        	while( SystemClock.uptimeMillis() < (toMillisec)) {
        		Log.e(TAG, "Up time millis : "+SystemClock.uptimeMillis());
//        		try {
//        			Thread.sleep(1000);
//        		}
//        		catch (InterruptedException ie) {
//        			
//        		}
        	}
        	
        	secondNote.stop();
        	Log.e(TAG, "Up time millisaaaa : "+SystemClock.uptimeMillis());
        	Log.d(TAG, "Play State: "+secondNote.getPlayState());
	    }
	}
    
}
